
'''
160. 相交链表
'''
from typing import Optional

from com.siyuan.ListNode import ListNode


class Solution:

    '''
    方法1：双指针（填充法）
    思路：链表1遍历完成后遍历连表2，链表2遍历完后遍历链表1，两指针相同时则是交点
    '''
    def getIntersectionNode1(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        p1 = headA
        p2 = headB
        while p1 != p2:
            if p1:
                p1 = p1.next
            else:
                p1 = headB

            if p2:
                p2 = p2.next
            else:
                p2 = headA
        return p1